package com.springcloud.serviceex;

import bcs.base.plc.PlcClient;
import bcs.base.plc.PlcClient.PlcClientListener;
import bcs.base.plc.PlcItem;
import bcs.base.plc.utils.PlcConstUtil;
import lombok.Data;


@Data
public class ForkliftService implements PlcClientListener, PlcConnect {
    private final PlcClient client;
    private final String SERVER = "AGV1";
    private final String BLOCK = "BLOCK1";
    private String actX;
    private String actY;
    private String actAngle;
    private String power;
    private static ForkliftService instance = null;

    public static ForkliftService getInstance() {
        if (instance == null) {
            instance = new ForkliftService();
        }
        return instance;
    }

    public ForkliftService() {
        client = new PlcClient(SERVER, "192.168.10.11", 102, 0, 2);
        client.setListener(this);
        client.setPrintContent(false);

        client.addBlock(BLOCK, 13, 0, 53);
        client.addItem(new PlcItem(SERVER, BLOCK, "POWER", PlcConstUtil.DataType.FLOAT, 40, 0, 0));
        client.addItem(new PlcItem(SERVER, BLOCK, "X_ACT", PlcConstUtil.DataType.FLOAT, 44, 0, 0));
        client.addItem(new PlcItem(SERVER, BLOCK, "Y_ACT", PlcConstUtil.DataType.FLOAT, 48, 0, 0));
        client.addItem(new PlcItem(SERVER, BLOCK, "YAW_ACT", PlcConstUtil.DataType.FLOAT, 52, 0, 0));


    }

    public void changedData(String name) {
        actX     = client.getItemValue("X_ACT");
        actY     = client.getItemValue("Y_ACT");
        actAngle = client.getItemValue("YAW_ACT");
        power    = client.getItemValue("POWER");
    }




    public boolean connect() {
        return client.connect();
    }

    public void disconnect() {
        client.disconnect();
    }

    public boolean isConnected() {
        return client.connected();
    }

    public boolean isNetOk() {
        return client.isNetOk();
    }

    public boolean isDataValid() {
        return client.isDataValid();
    }

    public void startPing() {
        client.startPing();
    }


    public void stopPing() {
        client.stopPing();
    }

    public void startRead() {
        client.startRead();
    }


    public void stopRead() {
        client.stopRead();
    }


}
